(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.group.updateController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'groupService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, groupService) {
		var ctrl = this, groupInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.group = {};
		ctrl.init = function() {
			ctrl.accessRolePermission = {permission : 'accessRole', authorization : authService};
			ctrl.accessBuPermission =  {permission : 'accessBu', authorization : authService};
			// init group data
			groupService.initPrincipalRoles($scope, ctrl);
			groupService.initParentGroups($scope, ctrl);
			groupService.initSubGroups($scope, ctrl);
			groupService.initGroupUsers($scope, ctrl);
			groupService.initBus($scope, ctrl);
			authService.update(groupInfo).then(function(response) {
				ctrl.group = response.data || groupInfo;
				ctrl.group.principalRoles = ctrl.group.principalRoles || [];
				ctrl.group.parentGroups = ctrl.group.parentGroups || [];
				ctrl.group.subGroups = ctrl.group.subGroups || [];
				ctrl.group.groupUsers = ctrl.group.groupUsers || [];
				ctrl.group.bus = ctrl.group.bus || [];
				groupService.getParentCompanyCodeOptions(ctrl).then(function(response) {
					ctrl.companyCodeOptions = response;
				}, angular.noop);
				ctrl.getPrincipalRoleList();
				ctrl.getParentGroupList();
				ctrl.getSubGroupList();
				ctrl.getGroupUserList();
			});
			
		};
		
		ctrl.getPrincipalRoleList = function() {
			authService.getPrincipalRoleList(groupInfo).then(function(response) {
				ctrl.group.principalRolesOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.principalRolesOld, ctrl.principalRoleOldGridOptions);
			});
		};
		
		ctrl.getParentGroupList = function() {
			authService.getParentGroupList(groupInfo).then(function(response) {
				ctrl.group.parentGroupsOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.parentGroupsOld, ctrl.parentGroupOldGridOptions);
			});
		};
		
		ctrl.getSubGroupList = function() {
			authService.getSubGroupList(groupInfo).then(function(response) {
				ctrl.group.subGroupsOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.subGroupsOld, ctrl.subGroupOldGridOptions);
			});
		};
		
		ctrl.getGroupUserList = function() {
			authService.getGroupUserList(groupInfo).then(function(response) {
				ctrl.group.groupUsersOld = response.data.list || [];
				gridService.updateGridOptions(ctrl.group.groupUsersOld, ctrl.userOldGridOptions);
			});
		};
		
		ctrl.updateSave = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.group.taskId) {
				return authService.updateContinueSave(ctrl.group).then(saveSuccessful, saveFailed);
			} else {
				return authService.updateSave(ctrl.group).then(saveSuccessful, saveFailed);
			}
		};

		ctrl.updateSubmit = function() {
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					'messages' : messages
				});
			} else if (ctrl.group.taskId) {
				return authService.updateContinueSubmit(ctrl.group).then(submitSuccessful, submitFailed);
			} else {
				return authService.updateSubmit(ctrl.group).then(submitSuccessful, submitFailed);
			}
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedSubCommpanyList = selectedRows;
		}

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.group.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.group.list',
					'state' : 'cda.group.list',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.group, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);